//
//  ActivityListViewController.m
//  豆瓣(阿黄)
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ActivityListViewController.h"
#import "ActivityListTableViewCell.h"
#import "AppTools.h"
#import "ActivityList.h"
#import "AcitivityDetailViewController.h"
#import "DataBase.h"

@interface ActivityListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)NSMutableDictionary *activityDic;

@property (nonatomic, retain)NSMutableArray *ownerArray;

@property (nonatomic, copy)NSString *ownerName;

@property (nonatomic, retain)NSMutableArray *activityArray;

@property (nonatomic, retain)NSMutableDictionary *bigDic;

@property (nonatomic, retain)NSData *picData;

@property (nonatomic, assign)NSString *document;

@property (nonatomic, retain)NSMutableArray *dataArray;

//@property (nonatomic, retain)NSMutableArray *activityArray2;

@end

@implementation ActivityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.activityTableView];
    [self.activityTableView release];
    
    [self.activityTableView registerClass:[ActivityListTableViewCell class] forCellReuseIdentifier:@"activityCell"];
    self.activityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.navigationItem.title = @"活动";
    
    //数据库
    [[DataBase shareDataBase] openDataBase];
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS activity(number INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, begin_time TEXT, end_time TEXT,address TEXT,category_name TEXT,wisher_count TEXT,image_hlarge TEXT,participant_count TEXT,content TEXT)";
    [[DataBase shareDataBase] createTable:createSql];
    self.dataArray = [[DataBase shareDataBase] selectActivity];
    self.activityDic = [NSMutableDictionary dictionary];
    self.activityArray = [NSMutableArray array];
    [self.activityDic setObject:self.dataArray forKey:@"events"];
    

    [AppTools getDataWithURLStr:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" Block:^(id consequnce) {
        
        NSString *dropSql = @"DROP TABLE activity";
        [[DataBase shareDataBase] dropTable:dropSql];
        NSString *createSql = @"CREATE TABLE IF NOT EXISTS activity(number INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, begin_time TEXT, end_time TEXT,address TEXT,category_name TEXT,wisher_count TEXT,image_hlarge TEXT,participant_count TEXT,content TEXT)";
        [[DataBase shareDataBase] createTable:createSql];
        self.bigDic = consequnce;
//        NSLog(@"%@",self.bigDic);
//        self.activityDic = [NSMutableDictionary dictionary];
        for (NSString *key in self.bigDic) {
            NSMutableArray *array = [self.bigDic objectForKey:@"events"];
//            self.activityArray = [NSMutableArray array];
            for (NSMutableDictionary *dic in array) {
                ActivityList *activityL = [[ActivityList alloc] init];
                [activityL setValuesForKeysWithDictionary:dic];
            
                NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO activity(title, begin_time, end_time, address, category_name, wisher_count, participant_count, content, image_hlarge) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", activityL.title, activityL.begin_time, activityL.end_time, activityL.address, activityL.category_name, activityL.wisher_count, activityL.participant_count, activityL.content, activityL.image_hlarge];
                [[DataBase shareDataBase] insertSql:insertSql];
                
                [self.activityArray addObject:activityL];
                //                NSLog(@"%@",self.activityArray);
                [activityL release];
            }
            [self.activityDic setObject:self.activityArray forKey:key];
        }
        [self.activityTableView reloadData];
//        self.activityArray2 = [NSMutableArray arrayWithArray:self.activityArray];
    }];
    

//    [AppTools getDataWithFileName:@"activitylist.txt" Block:^(id consequnce) {
//        
//        self.bigDic = consequnce;
//        self.activityDic = [NSMutableDictionary dictionary];
//        for (NSString *key in self.bigDic) {
//            NSMutableArray *array = [self.bigDic objectForKey:@"events"];
//            self.activityArray = [NSMutableArray array];
//            for (NSMutableDictionary *dic in array) {
//                ActivityList *activityL = [[ActivityList alloc] init];
//                [activityL setValuesForKeysWithDictionary:dic];
//                [self.activityArray addObject:activityL];
//                //                NSLog(@"%@",self.activityArray);
//                [activityL release];
//            }
//            
//            [self.activityDic setObject:self.activityArray forKey:key];
//            
//        }
//        //        NSLog(@"%@",self.activityDic);
//    }];

    
}

-(UITableView *)activityTableView{

    if (_activityTableView == nil) {
        self.activityTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        self.activityTableView.delegate = self;
        self.activityTableView.dataSource = self;
    }
    return _activityTableView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 190;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.activityArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellID = @"activityCell";
    ActivityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    
    ActivityList *activityL = [self.activityArray objectAtIndex:indexPath.row];
//    NSLog(@"%@",self.activityArray);
    
    //获取图片
//    NSString *urlStr = activityL.image_hlarge;
//    NSString *strEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    NSURL *url = [NSURL URLWithString:strEncode];
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    NSURLSession *session = [NSURLSession sharedSession];
//    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//        
////        NSLog(@"%@", data);
//        [cell sendData:data];
//        
//    }];
//    
//    [task resume];
    
    [cell sendValue:activityL];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:1];
    
    AcitivityDetailViewController *activityDVC = [[AcitivityDetailViewController alloc] init];
    activityDVC.detailIndex = indexPath;
    activityDVC.bigArray = self.activityArray;
    
//    NSMutableArray *array = [self.bigDic objectForKey:@"events"];
//    NSMutableDictionary *dic1 = [array objectAtIndex:indexPath.row];
//    NSMutableDictionary *dic2 = [dic1 objectForKey:@"owner"];
//    self.ownerName = [dic2 objectForKey:@"name"];
//    NSLog(@"%@",self.ownerName);
    activityDVC.ownerName = self.ownerName;
    
    
    
    [self.navigationController pushViewController:activityDVC animated:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
